<?php
namespace app\admin\controller;
use think\Controller;
use think\Request;
use catetree\Catetree;
class Conf extends Controller
{
	//配置项
	public function conflist()
	{
		$conf=db('conf');
		if(request()->isPost())
		{
			$files=$_FILES;
			$data=input('post.');
			//复选框空选的情况处理
			//先找出所有复选框的选项
			$checkboxRes=$conf->field('ename')->where('form_type','checkbox')->select();
			//将其转化为一维数组
			$checkboxArr=array();
			foreach ($checkboxRes as $k2 => $v2) {
					$checkboxArr[]=$v2['ename'];
			}
			//定义所有的提交字段
			$fieldsAll=array();
			//处理文字数据
			foreach ($data as $k => $v)
			{
				$fieldsAll[]=$k;
				if(is_array($v))
				{
					$value=implode(',',$v);
					$conf->where('ename',$k)->setField('value',$value);
				}else
				{
					$conf->where('ename',$k)->setField('value',$v);
				}
			}
			//如果复选框未选中任何一个,则置空
			foreach ($checkboxArr as $k3 => $v3) {
				//如果提交的字段中不含有复选框的字段则置为空
				if(!in_array($v3,$fieldsAll))
				{
					$conf->where('ename',$v3)->setField('value','');
				}
			}
			//处理图片数据
			if(!empty($files))
			{
				foreach ($files as $k1=> $v1)
				{
					//若未提交图片则保留不删除原来的
					if($v1['tmp_name']!=false)
					{
						$this->delbrandimg($k1);
						$filename=CONF_IMG_URL;
						$data[$k1]=uploadsingle($k1,$filename);
						$conf->where('ename',$k1)->setField('value',$data[$k1]);
					}
				}
			}
			$this->success('配置项配置成功');
			
		}
		
		$ShopConfRes=$conf->where('conf_type',1)->order('sort desc')->select();
		$GoodsConfRes=$conf->where('conf_type',2)->order('sort desc')->select();
		$settitle=settitle('配置项');
		$this->assign(
			array(
			'settitle'=>$settitle,
			'ShopConfRes'=>$ShopConfRes,
			'GoodsConfRes'=>$GoodsConfRes
			));
		return view();
	}
	public function lst()
	{	
		$cate=new Catetree();
		$confObj=db('conf');
		if(request()->isPost())
		{
			$data=input('post.');
			$result=$cate->cateSort($data['sort'],$confObj);
			if($result)
			{
				$this->success('排序成功',url('lst'));
			}else
			{
				$this->error('排序失败');
			}

		}
		$confRes=db('conf')->order('sort desc')->paginate('10');
		$settitle=settitle('配置');
		$this->assign(
		array(
			'settitle'=>$settitle,
			'confRes'=>$confRes
		));
		return view();
	}
	public function add()
	{		
		if(request()->isPost())
		{
			$data=input('post.');
			//如果是多选则 将中文逗号替换为英文',';
			if($data['form_type']=='radio'||$data['form_type']=='select'||$data['form_type']=='checkbox')
			{
				$data['values']=str_replace('，',',',$data['values']);
				$data['value']=str_replace('，',',',$data['value']);
			}
			//验证
    		$validate = validate('Conf');
    		if(!$validate->check($data)){
			    $this->error($validate->getError());
			}
			$add=db('conf')->insert($data);
			if($add)
			{
				$this->success('添加配置项成功',url('lst'));
			}else
			{
				$this->error('添加配置项失败');
			}
		}
		$settitle=settitle('配置');
		$this->assign('settitle',$settitle);
		return view();
	}

	public function edit()
	{	
		$id=input('id');
		if(request()->isPost())
		{	
			$data=input('post.');
			//如果是多选则 将中文逗号替换为英文',';
			if($data['form_type']=='radio'||$data['form_type']=='select'||$data['form_type']=='checkbox')
			{
				$data['values']=str_replace('，',',',$data['values']);
				$data['value']=str_replace('，',',',$data['value']);
			}
			//验证
    		$validate = validate('Conf');
    		if(!$validate->check($data)){
			    $this->error($validate->getError());
			}
			$save=db('conf')->update($data);
			if($save!==false)
			{
				$this->success('修改配置项成功',url('lst'));
			}else
			{
				$this->error('修改配置项失败');
			}
		}
		$confs=db('conf')->find($id);
		$settitle=settitle('配置');
		$this->assign(
			array(
			'confs'=>$confs,
			'settitle'=>$settitle,
		)
	);
		return view();
	}

	public function del($id)
	{	
		$del=db('conf')->delete($id);
		if($del)
		{
			$this->success('删除配置项成功',url('lst'));
		}else
		{
			$this->error('删除配置项失败');
		}
	}


	//配置项图片的删除
	public function delbrandimg($ename)
	{
		//查询是否有文章图片 如果有则删除
		$imgurl=db('conf')->field('value')->where('ename',$ename)->find();
		$imgurl=$imgurl['value'];
		if(!empty($imgurl))
		{
			$filename=CONF_IMG_URL.'\\'.$imgurl;
			if(file_exists($filename))
			{
				@unlink($filename);
			}
		}
	}




}
